package com.xingdata.jjxc.activity;

import jar.tools.UdpTools;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.xingdata.jjxc.App;
import com.xingdata.jjxc.Common;
import com.xingdata.jjxc.R;
import com.xingdata.jjxc.RequestCode;
import com.xingdata.jjxc.base.Base1Activity;
import com.xingdata.jjxc.enty.AddCarEntity;
import com.xingdata.jjxc.enty.CarBrandEntity;
import com.xingdata.jjxc.enty.CarInfoEntity;
import com.xingdata.jjxc.enty.RespEntity;
import com.xingdata.jjxc.http.HttpResponseListener;
import com.xingdata.jjxc.utils.SharedPreTools;
import com.xingdata.jjxc.utils.StringUtil;
import com.xingdata.jjxc.utils.SystemInfo;

/**
 * 添加爱车界面
 * 
 * @author Administrator
 * 
 */
public class CarInforAddActivity extends Base1Activity implements OnClickListener {
	
	final static String TAG = "CarInforAddActivity";
	
	private ImageView titleImageView;
	private Bundle mBundle;
	private TextView car_brand, car_frame_number, car_engine_number, car_type;
	private EditText car_number;// 车牌号
	private EditText car_mileage;// 里程数
	private List<CarBrandEntity> brandEntities = new ArrayList<CarBrandEntity>();
	private List<AddCarEntity> addCarEntities = new ArrayList<AddCarEntity>();

	private String branch_id;
	// 车辆品牌、车型号
	private String brandId, okresoult;
	private int flag;
	private Bundle bundle2, bundle3;
	private Bundle bundle;
	private Button button_sure;
	private String car_id, car_branch_name, car_model_id;
	private int add_flag = 0;// 0 新增 1 修改
	private CarInfoEntity carInfoEntity;
	private Intent intent = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		doPost_getcarbranchlist();
	}

	@Override
	protected void initViews() {
		((ImageView) findViewById(R.id.title_back)).setOnClickListener(this);
		titleImageView = (ImageView) findViewById(R.id.title_delet);
		findViewById(R.id.car_brand_layout).setOnClickListener(this);
		findViewById(R.id.car_type_layout).setOnClickListener(this);
		titleImageView.setOnClickListener(this);
		button_sure = (Button) findViewById(R.id.button_sure);
		button_sure.setOnClickListener(this);
		// 车牌号
		car_number = (EditText) findViewById(R.id.car_number);
		// 车型号
		car_type = (TextView) findViewById(R.id.car_type);
		car_brand = (TextView) findViewById(R.id.car_brand);
		// 车架号
		car_frame_number = (TextView) findViewById(R.id.car_frame_number);
		// 发动机号码
		car_engine_number = (TextView) findViewById(R.id.car_engine_number);
		// 里程数
		car_mileage = (EditText) findViewById(R.id.car_mileage);
		initData();
	}

	/**
	 * 初始化数据
	 */
	private void initData() {
		Bundle bundle = getIntent().getExtras();
		add_flag = bundle.getInt("add_flag");
		// 等于1的时候是修改
		if (add_flag == 1) {
			carInfoEntity = (CarInfoEntity) bundle.getSerializable("entity");
			titleImageView.setVisibility(View.VISIBLE);
			setViewData();
		} else {
			carInfoEntity = new CarInfoEntity();
		}
	}

	// 初始化修改爱车数据
	private void setViewData() {
		car_number.setText(carInfoEntity.getCar_license_no());
		car_number.setSelection(carInfoEntity.getCar_license_no().length());
		car_mileage.setText(carInfoEntity.getMileage_total());
		car_mileage.setSelection(carInfoEntity.getMileage_total().length());
		car_brand.setText(carInfoEntity.getCar_branch_name());
		car_frame_number.setText(carInfoEntity.getCar_vin_no());
		car_engine_number.setText(carInfoEntity.getCar_engine_no());
		car_type.setText(carInfoEntity.getCar_model_name());
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.title_back:
			finish();
			break;
		case R.id.title_delet:
			dopost_delcar();
			break;
		case R.id.button_sure:
			// carInfoEntity.setCar_branch_id(branch_id);
			
			carInfoEntity.setCar_license_no(car_number.getText().toString().trim());
			carInfoEntity.setCar_vin_no(car_frame_number.getText().toString().trim());
			carInfoEntity.setCar_engine_no(car_engine_number.getText().toString().trim());
			
			if (carInfoEntity.getCar_license_no().length() == 0) {
				showToast("车牌号不能为空");
				return;
			}

			if (carInfoEntity.getCar_vin_no().length() == 0) {
				showToast("车架号不能为空");
				return;
			}
			if (carInfoEntity.getCar_engine_no().length() == 0) {
				showToast("发动机号不能为空");
				return;
			}
			if (add_flag == 1) {
				doPost_UpdateCarInfo(App.ZZD_REQUEST_UPDATECAR);
			} else {
				doPost_UpdateCarInfo(App.ZZD_REQUEST_ADDCAR);
			}

			break;

		case R.id.car_brand_layout:
			Bundle bundle = new Bundle();
//			bundle.putParcelableArrayList("brand", (ArrayList<? extends Parcelable>) brandEntities);
			intent = new Intent(new Intent(this, CarBrandListViewActivity.class));
			intent.putExtras(bundle);
			startActivityForResult(intent, RequestCode.REQUESTCODE_CAR_BRAND);
			break;
		case R.id.car_type_layout:
			bundle2 = new Bundle();
//			bundle2.putParcelableArrayList("brand1", (ArrayList<? extends Parcelable>) addCarEntities);
//			if (carInfoEntity.getCar_branch_id() != null) {
//				bundle2.putString("brand_id", carInfoEntity.getCar_branch_id());
//				Intent intent2 = new Intent(new Intent(this, CarTypeActivity.class));
//				intent2.putExtras(bundle2);
//				startActivityForResult(intent2, RequestCode.REQUESTCODE_CAR_ID);
//			}
			break;

		default:
			break;
		}
	}

	private void dopost_delcar() {
		paramsMap = new HashMap<String, String>();
		// 用户id
		paramsMap.put("userid", SystemInfo.getUserInfo().getUserid());
		//效验码
		paramsMap.put("access_token", SystemInfo.getTokenEntity().getToken());
		//爱车id
		paramsMap.put("car_id", carInfoEntity.getCar_id());
		
		mHttpUtil.Post(App.ZZD_REQUEST_DELCAR, paramsMap, new HttpResponseListener() {

			@Override
			public void onSuccess(RespEntity respEntity) {
				finish();
			}

			@Override
			public void onLoading(int vurrent) {

			}

			@Override
			public void onFailure(String result) {
				showToast(result);
			}
		});
	}

	// // 更改爱车信息
	// private void dopost_updatecar() {
	// paramsMap = new HashMap<String, String>();
	// paramsMap.put("userid", SystemInfo.getUserInfo().getUserid());
	// paramsMap.put("access_token", SystemInfo.getTokenEntity().getToken());
	// paramsMap.put("hud_sn",
	// UdpTools.getHudCode() == null ? "" : UdpTools.getHudCode());
	// paramsMap.put("car_id", carInfoEntity.getCar_id());
	// paramsMap.put("car_license_no", car_number.getText().toString());
	// paramsMap.put("car_engine_no", car_engine_number.getText().toString());
	// paramsMap.put("car_vin_no", car_frame_number.getText().toString());
	// paramsMap.put("car_branch_id", carInfoEntity.getCar_branch_id());
	// // paramsMap.put("car_branch_name", car_branch_name);
	// paramsMap.put("car_model_id", carInfoEntity.getCar_model_id());
	// paramsMap.put("mileage_total", car_mileage.getText().toString());
	// mHttpUtil.Post(App.ZZD_REQUEST_UPDATECAR, paramsMap,
	// new HttpResponseListener() {
	//
	// @Override
	// public void onSuccess(RespEntity respEntity) {
	// System.out.println(respEntity.getMsg());
	// }
	//
	// @Override
	// public void onLoading(int vurrent) {
	//
	// }
	//
	// @Override
	// public void onFailure(String result) {
	//
	// }
	// });
	// }
	//
	// /**
	// * 添加爱车信息
	// */
	// private void doPost_addcar() {
	// paramsMap = new HashMap<String, String>();
	// paramsMap.put("userid", SystemInfo.getUserInfo().getUserid());
	// paramsMap.put("access_token", SystemInfo.getUserInfo().getToken());
	// if (!StringUtil.isStringNull(UdpTools.getHudCode())) {
	// paramsMap.put("hud_sn",
	// SharedPreTools.getString(this, Common.HUD_SN, ""));
	// } else {
	// paramsMap.put("hud_sn", UdpTools.getHudCode());
	// }
	// paramsMap.put("car_license_no", car_number.getText().toString());
	// paramsMap.put("car_engine_no", car_engine_number.getText().toString());
	// // 车架号
	// paramsMap.put("car_vin_no", car_frame_number.getText().toString());
	// // 品牌id
	// paramsMap.put("car_branch_id", carInfoEntity.getCar_branch_id());
	// // 型号id
	// paramsMap.put("car_model_id", carInfoEntity.getCar_model_id());
	// // 里程数
	// paramsMap.put("mileage_total", car_mileage.getText().toString());
	// mHttpUtil.Post(App.ZZD_REQUEST_ADDCAR, paramsMap,
	// new HttpResponseListener() {
	//
	// @Override
	// public void onSuccess(RespEntity respEntity) {
	// bundle = new Bundle();
	// bundle.putSerializable("addcarinfoentity",
	// carInfoEntity);
	// Intent intent = new Intent();
	// intent.putExtras(bundle);
	// setResult(RESULT_OK, intent);
	// finish();
	// }
	//
	// @Override
	// public void onLoading(int vurrent) {
	//
	// }
	//
	// @Override
	// public void onFailure(String result) {
	//
	// }
	// });
	//
	// }
	//

	/** 增删改查爱车信息到服务器 */
	private void doPost_UpdateCarInfo(String url) {
		paramsMap = new HashMap<String, String>();
		// 用户id
		paramsMap.put("userid", SystemInfo.getUserInfo().getUserid());
		// 校验码
		paramsMap.put("access_token", SystemInfo.getUserInfo().getToken());

		if (!StringUtil.isStringNull(UdpTools.getHudCode())) {
			// hud序列号
			paramsMap.put("hud_sn", SharedPreTools.getString(this, Common.HUD_SN, ""));
		} else {
			paramsMap.put("hud_sn", UdpTools.getHudCode());
		}
		if (add_flag == 0) {

		}
		// 车牌号
		paramsMap.put("car_license_no", car_number.getText().toString());
		// 发动机号
		paramsMap.put("car_engine_no", car_engine_number.getText().toString());
		// 车架号
		paramsMap.put("car_vin_no", car_frame_number.getText().toString());
		// 品牌id
		paramsMap.put("car_branch_id", carInfoEntity.getCar_branch_id());
		// 型号id
		paramsMap.put("car_model_id", carInfoEntity.getCar_model_id());
		// 里程数
		paramsMap.put("mileage_total", car_mileage.getText().toString());

		if (StringUtil.isStringNull(carInfoEntity.getCar_id())) {
			paramsMap.put("car_id", carInfoEntity.getCar_id());
		}
		mHttpUtil.Post(url, paramsMap, new HttpResponseListener() {

			@Override
			public void onSuccess(RespEntity respEntity) {
				Intent intent = new Intent();
				bundle = new Bundle();
				bundle.putSerializable("addcarinfoentity", carInfoEntity);
				intent.putExtras(bundle);
				setResult(RESULT_OK, intent);
				finish();
			}

			@Override
			public void onLoading(int vurrent) {

			}

			@Override
			public void onFailure(String result) {

			}
		});

	}

	/**
	 * 获取车辆品牌信息
	 */
	private void doPost_getcarbranchlist() {
		paramsMap = new HashMap<String, String>();
		paramsMap.put("pakege_name", this.getPackageName());
		mHttpUtil.Post(App.ZZD_REQUEST_GETCARBRANCHLIST, paramsMap, new HttpResponseListener() {

			@Override
			public void onSuccess(RespEntity respEntity) {
				brandEntities.addAll(JSON.parseArray(respEntity.getResult(), CarBrandEntity.class));
				// adapter.notifyDataSetChanged();
			}

			@Override
			public void onLoading(int vurrent) {

			}

			@Override
			public void onFailure(String result) {

			}
		});
	}

	@Override
	protected int getContentViewId() {
		return R.layout.carinfo_add;
	}

	@Override
	protected String getTitleText() {
		return "爱车";
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode != RESULT_OK) {
			return;
		}
		switch (requestCode) {
		case RequestCode.REQUESTCODE_CAR_BRAND:
			CarBrandEntity brandEntity = (CarBrandEntity) data.getExtras().getSerializable("entity");
			carInfoEntity.setCar_branch_id(brandEntity.getCar_branch_id());
			carInfoEntity.setCar_branch_name(brandEntity.getCar_branch_name());

			car_brand.setText(brandEntity.getCar_branch_name());
			// setViewData();
			break;
		case RequestCode.REQUESTCODE_CAR_ID:
			AddCarEntity addCarEntity = (AddCarEntity) data.getExtras().getSerializable("carEntity");
			carInfoEntity.setCar_model_id(addCarEntity.getCar_model_id());
			carInfoEntity.setCar_model_name(addCarEntity.getCar_model_name());
			branch_id = addCarEntity.getCar_model_name();
			car_type.setText(addCarEntity.getCar_model_name());
			// setViewData();

			break;
		default:
			break;
		}

	}
}
